﻿using DoNet.Core;
using DoNet.System.IServices;
using DoNet.System.Models;

namespace DoNet.Token
{
    /// <summary>
    /// 应用数据
    /// 可以改成数据库操作
    /// </summary>
    public class AppService : BaseService<App, AppOutputDto>, IAppService
    {
        public APPService(IRepository<App> appRepository)
        {
            repository = appRepository;
        }

        /// <summary>
        /// 同步新增实体。
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="trans">事务对象</param>
        /// <returns></returns>
        public override int Insert(App entity)
        {
            int result = repository.Insert(entity);
            this.UpdateCacheAllowApp();
            return result;
        }

        /// <summary>
        /// 异步更新实体。
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public override async Task<bool> UpdateAsync(App entity, string where)
        {
            bool result = await repository.UpdateAsync(entity, where);
            this.UpdateCacheAllowApp();
            return result;
        }
        /// <summary>
        /// 异步步新增实体。
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="trans">事务对象</param>
        /// <returns></returns>
        public override async Task<int> InsertAsync(App entity)
        {
            int result = await repository.InsertAsync(entity);
            this.UpdateCacheAllowApp();
            return result;
        }
        /// <summary>
        /// 获取app对象
        /// </summary>
        /// <param name="appid">应用ID</param>
        /// <param name="secret">应用密钥AppSecret</param>
        /// <returns></returns>
        public async Task<App> GetAPP(string appid, string secret)
        {
            return await repository.Db.Queryable<App>().Where(o => o.AppId == appid && o.AppSecret == secret).FirstAsync();
        }
        /// <summary>
        /// 获取app对象
        /// </summary>
        /// <param name="appid">应用ID</param>
        /// <returns></returns>
        public async Task<App> GetAPP(string appid)
        {
            return await repository.Db.Queryable<App>().Where(o => o.AppId == appid).FirstAsync();
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="search">查询的条件</param>
        /// <returns>指定对象的集合</returns>
        public override async Task<PageResult<AppOutputDto>> FindWithPagerAsync(SearchInputDto<APP> search)
        {
            bool order = search.Order == "asc" ? false : true;
            var exp = Expressionable.Create<App>()
               .AndIF(!string.IsNullOrEmpty(search.Keywords), it => it.AppId.Contains(search.Keywords) || it.RequestUrl.Contains(search.Keywords))
               .ToExpression();
            PagerInfo pagerInfo = new PagerInfo
            {
                CurrenetPageIndex = search.CurrenetPageIndex,
                PageSize = search.PageSize
            };
            List<App> list = await repository.FindWithPagerAsync(exp, pagerInfo, search.Sort, order);
            PageResult<AppOutputDto> pageResult = new PageResult<AppOutputDto>
            {
                CurrentPage = pagerInfo.CurrenetPageIndex,
                Items = list.MapTo<AppOutputDto>(),
                ItemsPerPage = pagerInfo.PageSize,
                TotalItems = pagerInfo.RecordCount
            };
            return pageResult;
        }
        /// <summary>
        /// 更新可用应用缓存
        /// </summary>
        public void UpdateCacheAllowApp()
        {
            IEnumerable<App> appList = repository.GetAllByIsNotDeleteAndEnabledMark();
            YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper();
            yuebonCacheHelper.Add(CacheConst.KeyAppList, appList);
        }
    }
}
